/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "freertos.h"
#include "task.h"
// #include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
__IO uint32_t uwTick = 0;
extern void freertos_init();
/* USER CODE END Includes */

/* Private function prototypes -----------------------------------------------*/
// void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void SystemInit(void)
{
}

void SystemClock_HSI_Init(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void delay(uint32_t delay)
{
//	volatile uint32_t max = uwTick + delay;
//	while (max - uwTick)
//		;
	vTaskDelay(delay);
}

void led_init()
{
	// PB2 PC13
	__HAL_RCC_GPIOC_CLK_ENABLE();
	__HAL_RCC_GPIOB_CLK_ENABLE();
	GPIOC->CRH &= ~(0XF0 << 16); // CLEAR CHR CNF13 MODE13
	GPIOC->CRH |= 0x10 << 16;	 // 推挽输出 PC13

	GPIOC->BSRR = 1 << 13; // set pc13

	GPIOC->BSRR = 1 << 13 << 16; // reset pc13

	GPIOB->CRL &= ~0X0F00;
	GPIOB->CRL |= 0X0100;

	GPIOB->BSRR = 1 << 2; // set pB2

	GPIOB->BSRR = 1 << 2 << 16; // reset pB2
}

void LED_ON()
{
	GPIOC->BSRR = 1 << 13 << 16; // reset pc13
}

void LED_OFF()
{
	GPIOC->BSRR = 1 << 13; // set pc13
}
/* USER CODE END 0 */

/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{

	/* USER CODE BEGIN 1 */
	SystemClock_HSI_Init(); 
	led_init();
	freertos_init();
	/* USER CODE BEGIN 1 */

	while (1)
	{
		delay(500);
		GPIOC->BSRR = 1 << 13; // set pc13
		GPIOB->BSRR = 1 << 2;
		delay(500);
		GPIOC->BSRR = 1 << 13 << 16; // reset pc13
		GPIOB->BSRR = 1 << 2 << 16;
	}
	/* USER CODE END 1 */
}

/* USER CODE BEGIN 4 */
void SystemClock_HSI_Init(void)
{
	// 1. 启用HSI并等待就绪
	RCC->CR |= RCC_CR_HSION;
	while (!(RCC->CR & RCC_CR_HSIRDY))
		;

	//// 2. 配置FLASH预取指和等待状态
	// FLASH->ACR |= FLASH_ACR_PRFTBE; // 启用预取指缓冲区
	// FLASH->ACR &= ~FLASH_ACR_LATENCY;
	// FLASH->ACR |= FLASH_ACR_LATENCY_1; // 1等待状态(适用于≤24MHz)

	// 3. 配置AHB/APB分频器
	RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2);
	RCC->CFGR |= RCC_CFGR_HPRE_DIV1;  // AHB不分频(8MHz)
	RCC->CFGR |= RCC_CFGR_PPRE1_DIV1; // APB1不分频(8MHz)
	RCC->CFGR |= RCC_CFGR_PPRE2_DIV1; // APB2不分频(8MHz) 
}
/* USER CODE END 4 */

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
	/* USER CODE BEGIN Error_Handler_Debug */
	/* User can add his own implementation to report the HAL error return state */
	// __disable_irq();
	while (1)
	{
	}
	/* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
	/* USER CODE BEGIN 6 */
	/* User can add his own implementation to report the file name and line number,
	   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
	/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
